
%% Get Image
close all
clc
%d0=imread('ImgA000000.tif');
%d1=imread('ImgA000001.tif');
%d1=frame21;
%   a = imread('smiley.jpg');                %read image
%    [m n]= size(d0);                          %get no of rows and column of the image matrix
    imshow(d0)                                %display original image
% 
% % Crop Image Using Submatrix Operation
    [y,x] = ginput(2);                       %select two cursor points
    r1 = x(1,1); c1 = y(1,1);                %get first cursor point = first corner of the rectangle
    r2 = x(2,1); c2 = y(2,1);               %get second cursor point = second corner of the rectangle




clear v1
clear v2
v1=d0(r1:r2,c1:c2,:);

v2=d1(r1:r2,c1:c2,:);

%figure


%v1=rgb2gray(v1);
%v2=rgb2gray(v2);

b1 = negative(v1);
b2 = negative(v2);

figure
imhist(b1)
thresholdValue = 160;
nb1 = b1 > thresholdValue; % Bright objects will be the chosen if you use >.
nb2 = b2 > thresholdValue; % Bright objects will be the chosen if you use >.

figure
imshow(nb1)
%figure
%imagesc(b2)


BW1 = bwareaopen(nb1, 130);
BW2 = bwareaopen(nb2, 130);
figure
imshow(BW1);

figure
imshow(BW2);
  
% 
% %v1 = negative(v1);
% figure
% imshow(v1)
% %v2 = negative(v2);
% figure
% imshow(v2)




icorr=xcorr2(double(BW1),double(BW2));%//,d1(r1:r2,c1:c2,:))
max_vec=max(icorr);
var=max(max(icorr));
maxx=find(max_vec==var);
max_vec=max(icorr');
var=max(max(icorr'));
maxy=find(max_vec==var);

[yorg xorg]=size(v1);
difx=maxx-xorg
dify=maxy-yorg

%ret=find(v(1,:)==max(v(1,:)))
%ret=ret/2;
figure
mesh(icorr)
ylabel('y')
xlabel('x')

size(v1);

ps=59.8/(646-12);
dy=ps*dify;

dx=ps*difx;
t=5/300;
vy=(dy/t)/1000

vx=(dx/t)/1000
%figure
%imshow(I0)
%get(f)

%hfig = figure;
%hax = axes;
%xdata = 1:100;
%ydata = sin(xdata);
%hline = line(xdata,ydata,'Marker','o');
 
%set(f,'ButtonDownFcn',@OnClickAxes);
 
 
%end
%% 

figure
imshow(frame15)

figure
imshow(frame16)
 
